#import "MiniMapView.h"
#import "Utility.h"

@implementation MiniMapView

static NSColor * mineralColor;
static NSColor * gasColor;

extern inline void NSRectScale( NSRect * rect, float scale_factor );

- (id)initWithFrame:(NSRect)frameRect
{
	if ((self = [super initWithFrame:frameRect]) != nil) {
		// Add initialization code here
		_imageRep = NULL;
		_map = NULL;
	}
	return self;
}

- (void)drawRect:(NSRect)rect
{
	if( _imageRep == NULL && _map != NULL ) {
		NSImage * image = [[NSImage alloc] initWithSize:NSMakeSize( [_map width], [_map height] )];
		[image lockFocus];
		_imageRep = [[NSBitmapImageRep alloc] initWithData:[image TIFFRepresentation]];
		[image unlockFocus];
		[image release];
	}
	
	if( mineralColor == NULL ) {
		mineralColor = [[NSColor colorWithCalibratedRed:0.0 green:1.0 blue:1.0 alpha:1.0] retain];
	}
	
	if( gasColor == NULL ) {
		gasColor = [[NSColor colorWithCalibratedRed:0.0 green:1.0 blue:0.0 alpha:1.0] retain];
	}
	
	
	[[NSColor blackColor] set];
	NSRectFill( rect );
	
	[[NSColor whiteColor] set];
	NSRect frameRect = NSMakeRect( (256 - [_map width]) / 2, (256 - [_map height]) / 2, [_map width], [_map height] ); 
	[[NSBezierPath bezierPathWithRect:frameRect] stroke];
	
	
	int X,Y;
	for( X = 0; X < [_map width]; X++ ) {
		for( Y = 0; Y < [_map height]; Y++ ) {
			u_int16_t mapTileID = [_map tileAtX:X Y:Y];
			int tileIndex = [_groupList indexForGroup:(mapTileID / 16) member:(mapTileID % 16)];
			[_imageRep setColor:[_tileList minimapColorForTileAtIndex:tileIndex] atX:X y:[_map height] - Y];
		}
	}
	
	int unitIndex;
	for( unitIndex = 0; unitIndex < [_unitList unitCount]; unitIndex++ ) {
		Unit * unit = [_unitList unitAtIndex:unitIndex];
		switch( [unit type] ) {
			case 176:
			case 177:
			case 178:
				[_imageRep setColor:mineralColor atX:[unit location].x/32 y:[unit location].y/32];
				break;
			case 188:
				[_imageRep setColor:gasColor atX:[unit location].x/32 y:[unit location].y/32];
				break;
			default:
				break;
		}
	}
	
	[_imageRep drawAtPoint:frameRect.origin];
	

	NSRect mainViewRect = [_mapView visibleMapRect];
	NSRect visibleView = NSMakeRect( frameRect.origin.x + ( mainViewRect.origin.x / 32 ) , frameRect.origin.y + ( mainViewRect.origin.y / 32 ), mainViewRect.size.width / 32, mainViewRect.size.height / 32 );
	NSRectScale( &visibleView , [[_scrollView documentView] scaleFactor] );
	[[NSBezierPath bezierPathWithRect:visibleView] stroke];
}

- (void)mouseDown:(NSEvent *)theEvent {
}

- (void)mouseDragged:(NSEvent *)theEvent {
	NSPoint point = [theEvent locationInWindow];
	NSRect frameRect = NSMakeRect( (256 - [_map width]) / 2, (256 - [_map height]) / 2, [_map width], [_map height] ); 
	if( NSPointInRect( point , frameRect ) ) {
		NSPoint scrollPoint = NSMakePoint( 32 * ( point.x - frameRect.origin.x - ([_mapView visibleMapRect].size.width / 64)) , 32 * (point.y - frameRect.origin.y - ([_mapView visibleMapRect].size.height / 64)) );
		if( scrollPoint.x + [_mapView visibleMapRect].size.width > [_map width] * 32 ) {
			scrollPoint.x = [_map width] * 32 - [_mapView visibleMapRect].size.width;
		}
		if( scrollPoint.y + [_mapView visibleMapRect].size.height > [_map height] * 32 ) {
			scrollPoint.y = [_map height] * 32 - [_mapView visibleMapRect].size.height;
		}
		[[_scrollView contentView] scrollToPoint:scrollPoint];
		[_scrollView reflectScrolledClipView: [_scrollView contentView]];
	}
	[[_scrollView window] makeFirstResponder:[_scrollView documentView]];
	[[_scrollView window] makeKeyWindow];
	[[_scrollView window] makeMainWindow];
}

- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent {
	NSPoint point = [theEvent locationInWindow];
	NSRect frameRect = NSMakeRect( (256 - [_map width]) / 2, (256 - [_map height]) / 2, [_map width], [_map height] ); 
	if( NSPointInRect( point , frameRect ) ) {
		NSPoint scrollPoint = NSMakePoint( 32 * ( point.x - frameRect.origin.x ) , 32 * (point.y - frameRect.origin.y) );
		if( scrollPoint.x + [_mapView visibleMapRect].size.width > [_map width] * 32 ) {
			scrollPoint.x = [_map width] * 32 - [_mapView visibleMapRect].size.width;
		}
		if( scrollPoint.y + [_mapView visibleMapRect].size.height > [_map height] * 32 ) {
			scrollPoint.y = [_map height] * 32 - [_mapView visibleMapRect].size.height;
		}
		[[_scrollView contentView] scrollToPoint:scrollPoint];
		[_scrollView reflectScrolledClipView: [_scrollView contentView]];
	}
	[[_scrollView window] makeFirstResponder:[_scrollView documentView]];
	[[_scrollView window] makeKeyWindow];
	[[_scrollView window] makeMainWindow];
	return YES;
}

- (BOOL)acceptsFirstResponder {
	return NO;
}

- (BOOL)becomeFirstResponder {
	return NO;
}

- (BOOL)resignFirstResponder {
	return YES;
}

- (void)setTileMap:(TileMap *)map {
	if( _map != map ) {
		[_map release];
		_map = [map retain];
	}
}

- (void)setTileList:(TileList *)tileList {
	if( _tileList != tileList ) {
		[_tileList release];
		_tileList = [tileList retain];
	}
}

- (void)setTileGroupList:(TileGroupList *)groupList {
	if( _groupList != groupList ) {
		[_groupList release];
		_groupList = [groupList retain];
	}
}

- (void)setPalette:(ColorPalette *)palette {
	if( _palette != palette ) {
		[_palette release];
		_palette = [palette retain];
	}
}

- (void)setUnitList:(UnitList *)unitList {
	if( _unitList != unitList ) {
		[_unitList release];
		_unitList = [unitList retain];
	}
}

@end
